<?php 
class productsDAL 
{
	public function Insert($db, $productsObj, &$errorMsg) 
	{
		$errorMsg = "";

		$query = "INSERT INTO products (code, name, description, price, unitID, cateID, subcateID, imageID, deleteflag) VALUES (".ToSQLPostVAL($productsObj->code).", ".ToSQLPostVAL($productsObj->name).", ".ToSQLPostVAL($productsObj->description).", ".ToSQLPostVAL($productsObj->price).", ".ToSQLPostVAL($productsObj->unitID).", ".ToSQLPostVAL($productsObj->cateID).", ".ToSQLPostVAL($productsObj->subcateID).", ".ToSQLPostVAL($productsObj->imageID).", ".ToSQLPostVAL($productsObj->deleteflag).")";
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		$productsObj->id = mysql_insert_id();
		return true;
	}

	public function Update($db, $productsObj, &$errorMsg)
	 {	
		$errorMsg = "";

		$query = "UPDATE products SET code=".ToSQLPostVAL($productsObj->code).", name=".ToSQLPostVAL($productsObj->name).", description=".ToSQLPostVAL($productsObj->description).", price=".ToSQLPostVAL($productsObj->price).", unitID=".ToSQLPostVAL($productsObj->unitID).", cateID=".ToSQLPostVAL($productsObj->cateID).", subcateID=".ToSQLPostVAL($productsObj->subcateID).", imageID=".ToSQLPostVAL($productsObj->imageID).", deleteflag=".ToSQLPostVAL($productsObj->deleteflag)." WHERE id=".ToSQLPostVAL($productsObj->id);
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		return true;
	}

	public function Load($db, $id, &$errorMsg, $joinTables="", $onFields="", $getFields="")
	{
		$errorMsg = "";
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$query	= "SELECT products.*$extraFields FROM products $joinClause WHERE products.id=".ToSQLPostVAL($id);

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
			return null;
		} else {
			if ($db->next_record()) {
				$productsObj = new productsObj($db->f("id"), $db->f("code"), $db->f("name"), $db->f("description"), $db->f("price"), $db->f("unitID"), $db->f("cateID"), $db->f("subcateID"), $db->f("imageID"), $db->f("deleteflag"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$productsObj->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				return $productsObj;
			} else {
				$errorMsg = "Data not found.";
				return null;
			}
		}
		return null;
	}

	public function IsExist($db, $whereStr) 
	{
		$query	= "SELECT * FROM products";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else return false;

if (!$db->query($query)) {
			return false;
		} else {
			while ($db->next_record()) {
				return true;
			}
		}
		return false;
	}

	public function Pure($db, $whereStr, &$errorMsg) 
	{
		$query	= "DELETE FROM products";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else {
			$errorMsg = "You have no delete condition in query string ($query WHERE ?).";
			return false;
		}

		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}
		return true;
	}

	public function GetList($db, $whereStr, $orderByStr, $limit, &$errorMsg, $joinTables="", $onFields="", $getFields="") 
	{
		$returnData = array();
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$errorMsg = "";
		$query	= "SELECT products.*$extraFields FROM products$joinClause";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		if ($orderByStr!="") $query = $query." ORDER BY ".$orderByStr;
		if ($limit!="") $query = $query." LIMIT ".$limit;

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
		} else {
			while ($db->next_record()) 	{
				$objVal = new productsObj($db->f("id"), $db->f("code"), $db->f("name"), $db->f("description"), $db->f("price"), $db->f("unitID"), $db->f("cateID"), $db->f("subcateID"), $db->f("imageID"), $db->f("deleteflag"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$objVal->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				$returnData[] = $objVal;
			}
		}
		return $returnData;
	}

	public function Push($productsObj)
	{
		global $id, $code, $name, $description, $price, $unitID, $cateID, $subcateID, $imageID, $deleteflag;

		$id = $productsObj->id;
		$code = $productsObj->code;
		$name = $productsObj->name;
		$description = $productsObj->description;
		$price = $productsObj->price;
		$unitID = $productsObj->unitID;
		$cateID = $productsObj->cateID;
		$subcateID = $productsObj->subcateID;
		$imageID = $productsObj->imageID;
		$deleteflag = $productsObj->deleteflag;
	}

	public function Pop(&$productsObj)
	{
		global $id, $code, $name, $description, $price, $unitID, $cateID, $subcateID, $imageID, $deleteflag;

		if (isset($id)) $productsObj->id = $id;
		if (isset($code)) $productsObj->code = $code;
		if (isset($name)) $productsObj->name = $name;
		if (isset($description)) $productsObj->description = $description;
		if (isset($price)) $productsObj->price = $price;
		if (isset($unitID)) $productsObj->unitID = $unitID;
		if (isset($cateID)) $productsObj->cateID = $cateID;
		if (isset($subcateID)) $productsObj->subcateID = $subcateID;
		if (isset($imageID)) $productsObj->imageID = $imageID;
		if (isset($deleteflag)) $productsObj->deleteflag = $deleteflag;
	}

} 
?>